RoleGrantRightView = Ext.extend(Ext.Window, {
	constructor : function(_cfg) {
		Ext.applyIf(this, _cfg);
		// 必须先初始化组件
		this.initUIComponents();
		RoleGrantRightView.superclass.constructor.call(this, {
					id : 'RoleGrantView',
					title : '角色授权设置',
					width : 600,
					tbar : this.tbar,
					height : 450,
					modal : true,
					layout : 'fit',
					plain : true,
					bodyStyle : 'padding:5px;',
					buttonAlign : 'center',
					items : [this.roleGrantView],
					buttons : [{
								text : '保存',
								iconCls : 'btn-save',
								scope : this,
								handler : this.saveRole
							}, {
								text : '取消',
								iconCls : 'btn-cancel',
								scope : this,
								handler : function() {
									this.close();
								}
							}]

				});
	},

	initUIComponents : function() {

		this.tbar = new Ext.Toolbar({
					items : [{
								xtype : 'button',
								text : '展开',
								iconCls : 'btn-expand',
								scope : this,
								handler : function() {
									this.roleGrantView.expandAll();
								}
							}, {
								xtype : 'button',
								text : '收起',
								iconCls : 'btn-collapse',
								scope : this,
								handler : function() {
									this.roleGrantView.collapseAll();
								}
							}]
				});

		// 使用AJAX
		var me = this;
		Ext.Ajax.request({
			url : __ctxPath + '/system/grantXmlAppRole.do',
			async : false,
			success : function(response, options) {
				var modules = Ext.decode(response.responseText);
				// 根据数据构建TreePanel
				me.roleGrantView = new Ext.tree.Panel({
					title : '为角色『' + me.roleName + '』授权',
					layout : 'fit',
					animate : false,
					border : false,
					useArrows: true,
					bubbleCheck : 'all',
					cascadeCheck : 'all',
					autoScroll : true,
					root : {
						expanded : true,
						text : "总菜单",
						children : modules.Modules.children
					},
					rootVisible : false,
					listeners : {
						checkchange : function(node, checked, obj) {
							// 选中所有子结点
							node.cascadeBy(function(n) {
										n.set('checked', checked);
									});
							checkParent(node);

							// 选中父结点
							function checkParent(node) {
								node = node.parentNode;
								if (!node)
									return;
								var checkP = false;
								node.cascadeBy(function(n) {
											if (n != node) {
												if (n.get('checked') == true) {
													checkP = true;
												}
											}
										});
								node.set('checked', checkP);
								checkParent(node);
							}
						},
						afterrender : function(view) {
							Ext.Ajax.request({
										url : __ctxPath + '/system/getAppRole.do',
										method : 'POST',
										params : {
											roleId : me.roleId
										},
										success : function(response, options) {
											var object = Ext.util.JSON.decode(response.responseText);
											if (object.data.rights != null) {
												walkTree(view.getRootNode(), object.data.rights.split(','));
											}

										},
										failure : function(response, options) {
											Ext.ux.Toast.msg('操作信息', '加载权限出错！');
										}
									});
									
							// 遍历所有的结点
							function walkTree(node, arr) {
								if(Ext.Array.contains(arr, node.id)) {
									node.set('checked', true);
								}
								Ext.Array.each(node.childNodes, function(n, idx) {
									walkTree(n, arr);
								});
							}
						}
					}
				});

			}
		});
	},
	// 保存
	saveRole : function() {
		var rights = [];
		var arr = this.roleGrantView.getChecked();
		Ext.Array.each(arr, function(node, index) {
			rights.push(node.id);
		});

		var me = this;
		Ext.Ajax.request({
					url : __ctxPath + '/system/grantAppRole.do',
					method : 'POST',
					params : {
						roleId : me.roleId,
						rights : rights.join(',')
					},
					success : function(response, options) {
						Ext.ux.Toast.msg('操作提示', '你已经成功为角色[<b>'+me.roleName+'</b>]进行了授权');
						me.close();
					},
					failure : function(response, options) {
						Ext.ux.Toast.msg('操作信息', '授权出错，请联系管理员！');
					}
				});
	}
});
